using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;


namespace Warehouse_Management
{
	public partial class HoSoChiTiet : ICloneable
	{
		#region Properties.
		
		public long ID { set; get; }
		public long HoSo_ID { set; get; }
		public string Barcode { set; get; }
		public decimal LuongTonDau { set; get; }
		public decimal SoLuongNhap { set; get; }
		public decimal SoLuongXuat { set; get; }
		public decimal LuongTonCuoi { set; get; }
		public decimal DoanhThu { set; get; }
		public decimal TyTrong { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static IList<HoSoChiTiet> ConvertToCollection(IDataReader reader)
		{
			IList<HoSoChiTiet> collection = new List<HoSoChiTiet>();
			while (reader.Read())
			{
				HoSoChiTiet entity = new HoSoChiTiet();
				if (!reader.IsDBNull(reader.GetOrdinal("ID"))) entity.ID = reader.GetInt64(reader.GetOrdinal("ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("HoSo_ID"))) entity.HoSo_ID = reader.GetInt64(reader.GetOrdinal("HoSo_ID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Barcode"))) entity.Barcode = reader.GetString(reader.GetOrdinal("Barcode"));
				if (!reader.IsDBNull(reader.GetOrdinal("LuongTonDau"))) entity.LuongTonDau = reader.GetDecimal(reader.GetOrdinal("LuongTonDau"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoLuongNhap"))) entity.SoLuongNhap = reader.GetDecimal(reader.GetOrdinal("SoLuongNhap"));
				if (!reader.IsDBNull(reader.GetOrdinal("SoLuongXuat"))) entity.SoLuongXuat = reader.GetDecimal(reader.GetOrdinal("SoLuongXuat"));
				if (!reader.IsDBNull(reader.GetOrdinal("LuongTonCuoi"))) entity.LuongTonCuoi = reader.GetDecimal(reader.GetOrdinal("LuongTonCuoi"));
				if (!reader.IsDBNull(reader.GetOrdinal("DoanhThu"))) entity.DoanhThu = reader.GetDecimal(reader.GetOrdinal("DoanhThu"));
				if (!reader.IsDBNull(reader.GetOrdinal("TyTrong"))) entity.TyTrong = reader.GetDecimal(reader.GetOrdinal("TyTrong"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		public static bool Find(IList<HoSoChiTiet> collection, long id)
        {
            foreach (HoSoChiTiet item in collection)
            {
                if (item.ID == id)
                {
                    return true;
                }
            }

            return false;
        }
		
		public static void UpdateDataSet(DataSet ds)
        {
            string insert = "Insert INTO t_WM_ThanhKhoan_HoSoChiTiet VALUES(@HoSo_ID, @Barcode, @LuongTonDau, @SoLuongNhap, @SoLuongXuat, @LuongTonCuoi, @DoanhThu, @TyTrong)";
            string update = "UPDATE t_WM_ThanhKhoan_HoSoChiTiet SET HoSo_ID = @HoSo_ID, Barcode = @Barcode, LuongTonDau = @LuongTonDau, SoLuongNhap = @SoLuongNhap, SoLuongXuat = @SoLuongXuat, LuongTonCuoi = @LuongTonCuoi, DoanhThu = @DoanhThu, TyTrong = @TyTrong WHERE ID = @ID";
            string delete = "DELETE FROM t_WM_ThanhKhoan_HoSoChiTiet WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@HoSo_ID", SqlDbType.BigInt, "HoSo_ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@Barcode", SqlDbType.VarChar, "Barcode", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LuongTonDau", SqlDbType.Decimal, "LuongTonDau", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongNhap", SqlDbType.Decimal, "SoLuongNhap", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongXuat", SqlDbType.Decimal, "SoLuongXuat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LuongTonCuoi", SqlDbType.Decimal, "LuongTonCuoi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@DoanhThu", SqlDbType.Decimal, "DoanhThu", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TyTrong", SqlDbType.Decimal, "TyTrong", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@HoSo_ID", SqlDbType.BigInt, "HoSo_ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@Barcode", SqlDbType.VarChar, "Barcode", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LuongTonDau", SqlDbType.Decimal, "LuongTonDau", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongNhap", SqlDbType.Decimal, "SoLuongNhap", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongXuat", SqlDbType.Decimal, "SoLuongXuat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LuongTonCuoi", SqlDbType.Decimal, "LuongTonCuoi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@DoanhThu", SqlDbType.Decimal, "DoanhThu", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TyTrong", SqlDbType.Decimal, "TyTrong", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, ds.Tables[0].TableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }
				
		public static void UpdateDataSet(DataSet ds, string tableName)
        {
            string insert = "Insert INTO t_WM_ThanhKhoan_HoSoChiTiet VALUES(@HoSo_ID, @Barcode, @LuongTonDau, @SoLuongNhap, @SoLuongXuat, @LuongTonCuoi, @DoanhThu, @TyTrong)";
            string update = "UPDATE t_WM_ThanhKhoan_HoSoChiTiet SET HoSo_ID = @HoSo_ID, Barcode = @Barcode, LuongTonDau = @LuongTonDau, SoLuongNhap = @SoLuongNhap, SoLuongXuat = @SoLuongXuat, LuongTonCuoi = @LuongTonCuoi, DoanhThu = @DoanhThu, TyTrong = @TyTrong WHERE ID = @ID";
            string delete = "DELETE FROM t_WM_ThanhKhoan_HoSoChiTiet WHERE ID = @ID";

			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();

            System.Data.Common.DbCommand InsertCommand = db.GetSqlStringCommand(insert);
			db.AddInParameter(InsertCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@HoSo_ID", SqlDbType.BigInt, "HoSo_ID", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@Barcode", SqlDbType.VarChar, "Barcode", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LuongTonDau", SqlDbType.Decimal, "LuongTonDau", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongNhap", SqlDbType.Decimal, "SoLuongNhap", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@SoLuongXuat", SqlDbType.Decimal, "SoLuongXuat", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@LuongTonCuoi", SqlDbType.Decimal, "LuongTonCuoi", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@DoanhThu", SqlDbType.Decimal, "DoanhThu", DataRowVersion.Current);
			db.AddInParameter(InsertCommand, "@TyTrong", SqlDbType.Decimal, "TyTrong", DataRowVersion.Current);

            System.Data.Common.DbCommand UpdateCommand = db.GetSqlStringCommand(update);
			db.AddInParameter(UpdateCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@HoSo_ID", SqlDbType.BigInt, "HoSo_ID", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@Barcode", SqlDbType.VarChar, "Barcode", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LuongTonDau", SqlDbType.Decimal, "LuongTonDau", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongNhap", SqlDbType.Decimal, "SoLuongNhap", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@SoLuongXuat", SqlDbType.Decimal, "SoLuongXuat", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@LuongTonCuoi", SqlDbType.Decimal, "LuongTonCuoi", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@DoanhThu", SqlDbType.Decimal, "DoanhThu", DataRowVersion.Current);
			db.AddInParameter(UpdateCommand, "@TyTrong", SqlDbType.Decimal, "TyTrong", DataRowVersion.Current);
			
            System.Data.Common.DbCommand DeleteCommand = db.GetSqlStringCommand(delete);
			db.AddInParameter(DeleteCommand, "@ID", SqlDbType.BigInt, "ID", DataRowVersion.Current);

            db.UpdateDataSet(ds, tableName, InsertCommand, UpdateCommand, DeleteCommand, UpdateBehavior.Standard);
        }

		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static HoSoChiTiet Load(long id)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, id);
            IDataReader reader = db.ExecuteReader(dbCommand);
			IList<HoSoChiTiet> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static IList<HoSoChiTiet> SelectCollectionAll()
		{
			IDataReader reader = SelectReaderAll();
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static IList<HoSoChiTiet> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IList<HoSoChiTiet> SelectCollectionBy_HoSo_ID(long hoSo_ID)
		{
            IDataReader reader = SelectReaderBy_HoSo_ID(hoSo_ID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_HoSo_ID(long hoSo_ID)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_SelectBy_HoSo_ID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, hoSo_ID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll()
        {
            const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll()
        {
            const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_HoSo_ID(long hoSo_ID)
		{
			const string spName = "p_WM_ThanhKhoan_HoSoChiTiet_SelectBy_HoSo_ID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, hoSo_ID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static long InsertHoSoChiTiet(long hoSo_ID, string barcode, decimal luongTonDau, decimal soLuongNhap, decimal soLuongXuat, decimal luongTonCuoi, decimal doanhThu, decimal tyTrong)
		{
			HoSoChiTiet entity = new HoSoChiTiet();	
			entity.HoSo_ID = hoSo_ID;
			entity.Barcode = barcode;
			entity.LuongTonDau = luongTonDau;
			entity.SoLuongNhap = soLuongNhap;
			entity.SoLuongXuat = soLuongXuat;
			entity.LuongTonCuoi = luongTonCuoi;
			entity.DoanhThu = doanhThu;
			entity.TyTrong = tyTrong;
			return entity.Insert();
		}
		
		public long Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public long Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@ID", SqlDbType.BigInt, 8);
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, HoSo_ID);
			db.AddInParameter(dbCommand, "@Barcode", SqlDbType.VarChar, Barcode);
			db.AddInParameter(dbCommand, "@LuongTonDau", SqlDbType.Decimal, LuongTonDau);
			db.AddInParameter(dbCommand, "@SoLuongNhap", SqlDbType.Decimal, SoLuongNhap);
			db.AddInParameter(dbCommand, "@SoLuongXuat", SqlDbType.Decimal, SoLuongXuat);
			db.AddInParameter(dbCommand, "@LuongTonCuoi", SqlDbType.Decimal, LuongTonCuoi);
			db.AddInParameter(dbCommand, "@DoanhThu", SqlDbType.Decimal, DoanhThu);
			db.AddInParameter(dbCommand, "@TyTrong", SqlDbType.Decimal, TyTrong);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				ID = (long) db.GetParameterValue(dbCommand, "@ID");
				return ID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<HoSoChiTiet> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HoSoChiTiet item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateHoSoChiTiet(long id, long hoSo_ID, string barcode, decimal luongTonDau, decimal soLuongNhap, decimal soLuongXuat, decimal luongTonCuoi, decimal doanhThu, decimal tyTrong)
		{
			HoSoChiTiet entity = new HoSoChiTiet();			
			entity.ID = id;
			entity.HoSo_ID = hoSo_ID;
			entity.Barcode = barcode;
			entity.LuongTonDau = luongTonDau;
			entity.SoLuongNhap = soLuongNhap;
			entity.SoLuongXuat = soLuongXuat;
			entity.LuongTonCuoi = luongTonCuoi;
			entity.DoanhThu = doanhThu;
			entity.TyTrong = tyTrong;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_WM_ThanhKhoan_HoSoChiTiet_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, ID);
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, HoSo_ID);
			db.AddInParameter(dbCommand, "@Barcode", SqlDbType.VarChar, Barcode);
			db.AddInParameter(dbCommand, "@LuongTonDau", SqlDbType.Decimal, LuongTonDau);
			db.AddInParameter(dbCommand, "@SoLuongNhap", SqlDbType.Decimal, SoLuongNhap);
			db.AddInParameter(dbCommand, "@SoLuongXuat", SqlDbType.Decimal, SoLuongXuat);
			db.AddInParameter(dbCommand, "@LuongTonCuoi", SqlDbType.Decimal, LuongTonCuoi);
			db.AddInParameter(dbCommand, "@DoanhThu", SqlDbType.Decimal, DoanhThu);
			db.AddInParameter(dbCommand, "@TyTrong", SqlDbType.Decimal, TyTrong);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<HoSoChiTiet> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HoSoChiTiet item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateHoSoChiTiet(long id, long hoSo_ID, string barcode, decimal luongTonDau, decimal soLuongNhap, decimal soLuongXuat, decimal luongTonCuoi, decimal doanhThu, decimal tyTrong)
		{
			HoSoChiTiet entity = new HoSoChiTiet();			
			entity.ID = id;
			entity.HoSo_ID = hoSo_ID;
			entity.Barcode = barcode;
			entity.LuongTonDau = luongTonDau;
			entity.SoLuongNhap = soLuongNhap;
			entity.SoLuongXuat = soLuongXuat;
			entity.LuongTonCuoi = luongTonCuoi;
			entity.DoanhThu = doanhThu;
			entity.TyTrong = tyTrong;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, ID);
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, HoSo_ID);
			db.AddInParameter(dbCommand, "@Barcode", SqlDbType.VarChar, Barcode);
			db.AddInParameter(dbCommand, "@LuongTonDau", SqlDbType.Decimal, LuongTonDau);
			db.AddInParameter(dbCommand, "@SoLuongNhap", SqlDbType.Decimal, SoLuongNhap);
			db.AddInParameter(dbCommand, "@SoLuongXuat", SqlDbType.Decimal, SoLuongXuat);
			db.AddInParameter(dbCommand, "@LuongTonCuoi", SqlDbType.Decimal, LuongTonCuoi);
			db.AddInParameter(dbCommand, "@DoanhThu", SqlDbType.Decimal, DoanhThu);
			db.AddInParameter(dbCommand, "@TyTrong", SqlDbType.Decimal, TyTrong);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<HoSoChiTiet> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HoSoChiTiet item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteHoSoChiTiet(long id)
		{
			HoSoChiTiet entity = new HoSoChiTiet();
			entity.ID = id;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@ID", SqlDbType.BigInt, ID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_HoSo_ID(long hoSo_ID)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_DeleteBy_HoSo_ID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@HoSo_ID", SqlDbType.BigInt, hoSo_ID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_WM_ThanhKhoan_HoSoChiTiet_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<HoSoChiTiet> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (HoSoChiTiet item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
		
		
        #region ICloneable Members

        public object Clone()
        {
            return base.MemberwiseClone();
        }

        #endregion
	}	
}